package com.leetcode.August;

/**
 * @Description: 代码 测试用例 测试结果 测试结果 86. 分隔链表
 * @Author: Mr.Fxy
 * @CreateTime: 2024-09-01 20:22
 */
public class LC86分隔链表 {
    public ListNode partition(ListNode head, int x) {
        if(head==null||head.next==null) return head;
        ListNode node1 = new ListNode(0);
        ListNode node2 = new ListNode(0);
        ListNode left=node1;
        ListNode right = node2;
        while(head!=null){
            if (head.val<x){
                left.next = head;
                left=head;//更新为尾节点
            }else{
                right.next = head;
                right=head;
            }
            head=head.next;
        }
        left.next=node2.next;
        return node1.next;
    }
}
